<?php

namespace App\HttpController\Web;

use AlibabaCloud\Client\AlibabaCloud;
use App\HttpController\BaseController;
use App\HttpController\ViewController;
use App\Model\FoodsModel;
use App\Model\OrdersModel;
use App\Model\ShopsModel;
use App\Utility\Alisms;
use App\Utility\ApiToken;
use App\Utility\ExcelClient;
use App\Utility\MiniProgram;
use App\Utility\Pool\MysqlObject;
use App\Utility\Pool\MysqlPool;
use easySwoole\Cache\Cache;
use EasySwoole\EasySwoole\ServerManager;
use EasySwoole\Pay\WeChat\Config;


class PayController extends ViewController{

    private $wechatConfig;

    public function __construct()
    {
        $wechatConfig = new Config();
        $wechatConfig->setAppId('');
        $wechatConfig->setMchId('');
        $wechatConfig->setKey('');
        $wechatConfig->setNotifyUrl('');
        $wechatConfig->setApiClientCert('');
        $wechatConfig->setApiClientKey('');
        $this->wechatConfig = $wechatConfig;
    }

    function index()
    {
//        $request = $this->request();
//        $id = $request->getRequestParam('id');
        $orderSn = 'CN' . date('YmdHis') . rand(1000, 9999);
        $wap = new \EasySwoole\Pay\WeChat\RequestBean\Wap();
        $wap->setOutTradeNo($orderSn);
        $wap->setBody('商品支付测试' . $orderSn);
        $wap->setTotalFee(1);
        $wap->setSpbillCreateIp($this->getIp());
        $pay = new \EasySwoole\Pay\Pay();
        try {
            $params = $pay->weChat($this->wechatConfig)->wap($wap);
            $params = $params->toArray();
            $str = '<html>
<head></head>
<body>
<script>
    window.location.href="' . $params['mweb_url'] . '";
</script></body></body></html>';
            $this->response()->write($str);
        } catch (\Exception $e) {
            echo $e->getMessage();
        }
    }
}
